package com.czk.springcloud.controller;

import com.czk.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/18 15:55
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    /**
     * 没有延迟的方法
     *
     * @param id
     * @return
     */
    @GetMapping("/hytrix/ok/{id}")
    public String payment_ok(@PathVariable("id") Integer id) {
        log.info("调用ok方法");
        String s = paymentService.paymentInfo_Ok(id);
        return s;
    }

    /**
     * 高并发请求时，会导致ok没有延迟的方法有延迟返回现象
     *
     * @param id
     * @return
     */
    @GetMapping("/hytrix/fail/{id}")
    public String payment_fail(@PathVariable("id") Integer id) {
        log.info("调用fail方法");
        String s = null;
        try {
            s = paymentService.paymentInfo_Fail(id);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return s;
    }
    //========服务熔断
    @GetMapping("/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String s = paymentService.paymentCircuitBreaker(id);
        return s;
    }
}
